package cn.edu.cug.cs.gtl.docsrv.auth.dao.imp;

import cn.edu.cug.cs.gtl.docsrv.auth.dao.ResourceDao;
import cn.edu.cug.cs.gtl.docsrv.config.Configuration;
import cn.edu.cug.cs.gtl.mybatis.Session;
import cn.edu.cug.cs.gtl.mybatis.mapper.auth.AuthResource;
import cn.edu.cug.cs.gtl.mybatis.mapper.auth.AuthResourceMapper;
import org.springframework.stereotype.Component;

@Component
public class ResourceDaoImpl implements ResourceDao {
    @Override
    public boolean insertResource(AuthResource authResource) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthResourceMapper  mapper = s.getMapper(AuthResourceMapper.class);
            mapper.insert(authResource);
            s.close();
            return true;
        }
        return false;
    }

    @Override
    public boolean updateResource(AuthResource authResource) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthResourceMapper  mapper = s.getMapper(AuthResourceMapper.class);
            mapper.updateByResourceName(authResource);
            s.close();
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteResource(String authResourceName) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthResourceMapper  mapper = s.getMapper(AuthResourceMapper.class);
            mapper.deleteByResourceName(authResourceName);
            s.close();
            return true;
        }
        return false;
    }

    @Override
    public AuthResource getResource(String authResourceName) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthResourceMapper  mapper = s.getMapper(AuthResourceMapper.class);
            AuthResource authResource = mapper.selectByResourceName(authResourceName);
            s.close();
            return authResource;
        }
        return null;
    }
}
